#ifndef MAIN_H
#define MAIN_H


#include <QApplication>
#include <QtGui>
#include <QtCore> 
#include <QTextCodec>
#include <QCoreApplication>





/* read the contenet of a local file as qstring */
extern inline QString DDopen(QString fullFileName)
{
    QString inside = "";
    QFile file(fullFileName); 
    if (file.exists()) {
                if (file.open(QFile::ReadOnly | QFile::Text)) {
                    inside = QString::fromUtf8(file.readAll());
                  file.close();
                }
    }

return inside;
}


#define NO_TEXT_FILE_PAGE \
              QString( DDopen(QString::fromUtf8(":/img/_notext.html")) )








/* xml */
#include <QDomDocument>



/* write a file to utf-8 format */
static inline bool debugwrite(QString fullFileName,QString xml)
{
    if (fullFileName.contains("/", Qt::CaseInsensitive)) {
    QString ultimacartellaaperta = fullFileName.left(fullFileName.lastIndexOf("/"))+"/";
    QDir dira(ultimacartellaaperta);
    if ( dira.mkpath(ultimacartellaaperta) ) { } else {
    return false;
    }
    }
    
        QTextCodec *codecx;
        codecx = QTextCodec::codecForMib(106);
        QFile f( fullFileName );
        if ( f.open( QFile::WriteOnly | QFile::Text ) )
        {
        QTextStream sw( &f );
        sw.setCodec(codecx);
        sw << xml;
        f.close();
        return true;
        }
        return false;
}

static inline QString HumanTextQuiet( QString todomdocument , int compression )
{
    QString errorStr;
    int errorLine, errorColumn;
    QDomDocument doc;
    
    if (!doc.setContent(todomdocument,false, &errorStr, &errorLine, &errorColumn)) {
         todomdocument = todomdocument.replace("\n","");
         todomdocument = todomdocument.replace("\t","");
         todomdocument = todomdocument.replace("\r","");
      return todomdocument;
     }
     
     
     QString canse = doc.toString(compression);
     if (compression == 0) {
         canse = canse.replace("\n","");
         canse = canse.replace("\t","");
         canse = canse.replace("\r","");
     }
     return canse;
}




#endif

